import * as actionTypes from './actionTypes';

const defaultState = {
    inputValue: '',
    list: []
};

// reducer可以接受state，但是绝不能修改state
export default (state = defaultState, action) => {
    // console.log(state, action);
    if(action.type === actionTypes.CHANGE_INPUT_VALUE){
        const newState = JSON.parse(JSON.stringify(state));
        newState.inputValue = action.value;
        return newState;
    }

    if(action.type === actionTypes.ADD_TODO_ITEM){
        const newState = JSON.parse(JSON.stringify(state));
        newState.inputValue ? newState.list.push(newState.inputValue) : alert('请输入内容');
        newState.inputValue = '';
        return newState;
    }

    if(action.type === actionTypes.DELETE_TODO_ITEM){
        const newState = JSON.parse(JSON.stringify(state));
        newState.list.splice(action.index,1)
        return newState;
    }

    return state;
}